package com.example.back.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.back.common.Result;
import com.example.back.entity.Criterion;
import com.example.back.entity.CriterionPage;
import com.example.back.entity.CriterionTemplates;
import com.example.back.entity.User;
import com.example.back.exception.ServiceException;
import com.example.back.service.CriterionService;
import com.example.back.service.TemplateService;
import com.example.back.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.logging.Level;

/**
 * @author Jm
 * @date 2024/7/17
 */
@RestController
@RequestMapping("/criterion")
public class CriterionController
{
    @Autowired
    private CriterionService criterionService;

    @Autowired
    private TemplateService templateService;

    @GetMapping("/list")
    public Result list()
    {
        return Result.success(criterionService.list());
    }

    @GetMapping("/templateList")
    public Result tempList()
    {
        return Result.success(templateService.list());
    }

    @PostMapping("/addTemplate")
    public Result addTemplate(@RequestBody CriterionTemplates criterionTemplates)
    {
        templateService.save(criterionTemplates);
        return Result.success();
    }

    @PostMapping("/add")
    public Result add(@RequestBody Criterion criterion)
    {
        criterion.setId(null);
        criterionService.addCriterion(criterion);
        return Result.success();
    }


    @PostMapping("/selectPage")
    public Result selectByPage(@RequestBody CriterionPage criterionPage)
    {
        int pageNum = criterionPage.getPageNum();
        int pageSize = criterionPage.getPageSize();

        QueryWrapper<Criterion> queryWrapper = new QueryWrapper<Criterion>();
        queryWrapper.eq(criterionPage.getLevel() != null, "level", criterionPage.getLevel());
        queryWrapper.eq(criterionPage.getTemplateId() != null, "template_id", criterionPage.getTemplateId());
        Page<Criterion> page = criterionService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id)
    {
        criterionService.removeById(id);
        return Result.success();
    }

    @GetMapping("/selectTree/{id}/{userId}")
    public Result selectTree(@PathVariable Integer id,
                             @PathVariable Integer userId)
    {
        return Result.success(criterionService.selectTree(id, userId));
    }


    @GetMapping("/selectScoreTree")
    public Result selectScoreTree(@RequestParam Integer id,
                                  @RequestParam Integer userId)
    {
        return Result.success(criterionService.selectScoreTree(id, userId));
    }

    @GetMapping("/selectAllScore")
    public Result selectAllScore(@RequestParam Integer id,
                                  @RequestParam Integer userId)
    {
        return Result.success(criterionService.selectAllScore(id, userId));
    }

}
